//
//  UIView+UIGetureRecognizer.m
//  ZhaoCaiMao
//
//  Created by xieyingze on 2017/3/14.
//  Copyright © 2017年 谢英泽. All rights reserved.
//

#import "UIView+UIGetureRecognizer.h"
#import <objc/runtime.h>

static char completeStr = 'a';

@implementation UIView (UIGetureRecognizer)

- (kGetureComplete)gestureComplete {
    return objc_getAssociatedObject(self, &completeStr);
}

- (void)setGestureComplete:(kGetureComplete)gestureComplete {
    objc_setAssociatedObject(self, &completeStr, gestureComplete, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

#pragma mark - *************************点击手势*************************
/**
 点击手势
 */
- (void)addTapGetureRecognizer {
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapView:)];
    [self addGestureRecognizer:tap];
}

- (void)tapView:(UITapGestureRecognizer *)tap {
    if (self.gestureComplete) {
        self.gestureComplete();
    }
}

#pragma mark - *************************拖动手势*************************
/**
 向下拖动手势
 */
- (void)addPanGetureBottomRecohnizer {
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panBottomView:)];
    [self addGestureRecognizer:pan];
}

- (void)panBottomView:(UIPanGestureRecognizer *)pan {
    if (pan.state == UIGestureRecognizerStateChanged) {
        [self commitTranslation:[pan translationInView:self.superview] direction:0];
    }
}

/**
 向上拖动手势
 */
- (void)addPanGetureTopRecohnizer {
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panTopView:)];
    [self addGestureRecognizer:pan];
}

- (void)panTopView:(UIPanGestureRecognizer *)pan {
    if (pan.state == UIGestureRecognizerStateChanged) {
        [self commitTranslation:[pan translationInView:self.superview] direction:1];
    }
}

/**
 向左拖动手势
 */
- (void)addPanGetureLeftRecohnizer {
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panLeftView:)];
    [self addGestureRecognizer:pan];
}

- (void)panLeftView:(UIPanGestureRecognizer *)pan {
    if (pan.state == UIGestureRecognizerStateChanged) {
        [self commitTranslation:[pan translationInView:self.superview] direction:2];
    }
}

/**
 向右拖动手势
 */
- (void)addPanGetureRightRecohnizer {
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panRightView:)];
    [self addGestureRecognizer:pan];
}

- (void)panRightView:(UIPanGestureRecognizer *)pan {
    if (pan.state == UIGestureRecognizerStateChanged) {
        [self commitTranslation:[pan translationInView:self.superview] direction:3];
    }
}

/**
 *   判断手势方向
 *
 *  @param translation translation description
 */
- (void)commitTranslation:(CGPoint)translation direction:(NSInteger)direction {
    /**
     direction 0 为向下
     direction 1 为向上
     direction 2 为向左
     direction 3 为向右
     */
    CGFloat absX = fabs(translation.x);
    CGFloat absY = fabs(translation.y);
    
    // 设置滑动有效距离
    if (MAX(absX, absY) < 64)
        return;
    
    if (absX > absY ) {
        if (translation.x<0) {
            //向左滑动
            if (direction == 2) {
                if (self.gestureComplete) {
                    self.gestureComplete();
                }
            }
        }else{
            //向右滑动
            if (direction == 3) {
                if (self.gestureComplete) {
                    self.gestureComplete();
                }
            }
        }
        
    } else if (absY > absX) {
        //向上滑动
        if (translation.y<0) {
            if (direction == 1) {
                if (self.gestureComplete) {
                    self.gestureComplete();
                }
            }
        }else{
            //向下滑动
            if (direction == 0) {
                if (self.gestureComplete) {
                    self.gestureComplete();
                }
            }
        }
    }
}

@end
